package com.jshan.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshan.base.entity.SysContent;
import com.jshan.mapper.SysContentMapper;
import com.jshan.service.SysContentService;
import com.jshan.base.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 17559
 * @description 针对表【sys_content】的数据库操作Service实现
 * @createDate 2025-06-24 13:33:09
 */
@Service
@Slf4j
public class SysContentServiceImpl extends ServiceImpl<SysContentMapper, SysContent>
        implements SysContentService {

    /**
     * 分页查询文章列表
     *
     * @param pageNum  第几页
     * @param pageSize 分页数量
     * @param title    查询条件
     * @return
     */
    @Override
    public Result findPage(Integer pageNum, Integer pageSize, String title) {
        Page<SysContent> sysContentPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysContent> w = new LambdaQueryWrapper<>();
        if (log.isInfoEnabled()) {
            log.info("正在进行文章分页查询：{}-{}, 查询条件：{}", pageNum, pageSize, title);
        }
        if (StringUtils.isNotBlank(title)) {
            w.like(SysContent::getTitle, title);
        }
        return Result.ok(this.page(sysContentPage, w));
    }

    /**
     * 更新或者添加文章
     *
     * @param content 内容
     * @return boolean
     */
    @Override
    public boolean saveAndUpdate(SysContent content) {
        if (content.getId() == null) {
            content.setCreateId(StpUtil.getLoginIdAsInt());
            content.setCreateTime(LocalDateTime.now());
        } else {
            content.setUpdateId(StpUtil.getLoginIdAsInt());
            content.setUpdateTime(LocalDateTime.now());
        }
        return this.saveOrUpdate(content);
    }
}




